#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "showpid.h"
#include <QDebug>
using namespace std;


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , serial(new QSerialPort(this))
    , timer(new QTimer())
{
    ui->setupUi(this);
    open_flag=0;
    x_index=0;
    connect(timer, &QTimer::timeout, this, &MainWindow::time_out);
    timer->start(100);

    /**************************************************************/
    //https://blog.csdn.net/weixin_58619060/article/details/135111607

    chart = new QChart();

    series1 = new QSplineSeries();
    series2 = new QLineSeries();

    series1->setName("曲线");
    series2->setName("折线");

    axisX = new QValueAxis();
    axisY = new QValueAxis();

    chart->addAxis(axisX,Qt::AlignBottom);
    chart->addAxis(axisY,Qt::AlignLeft);

    axisX->setTickCount(11);
    axisX->setRange(0, 10);
    axisY->setRange(0, 20);

    //1
    chart->addSeries(series1);
    chart->addSeries(series2);
    //2
    series1->attachAxis(axisX);
    series1->attachAxis(axisY);

    series2->attachAxis(axisX);
    series2->attachAxis(axisY);

    ui->pidshow->setChart(chart);





}

MainWindow::~MainWindow()
{
    delete ui;
    delete serial;

    delete axisX;
    delete axisY;
    delete series1;
    delete series2;
    delete chart;
}

uint8_t MainWindow::get_open_flag()
{
    return open_flag;
}
void MainWindow::set_open_flat(uint8_t data)
{
    open_flag=data;
}

void MainWindow::time_out()
{
    //qDebug()<<"time_out";
    //QByteArray sendData = "Hello, Serial Port!";

    if(serial->isOpen())
    {
        //serial->write(sendData);
        QByteArray sendData_ByteArray=serial->readLine();


        //qDebug()<<sendData_ByteArray.size();
        //qDebug()<<sendData_ByteArray;
        if(sendData_ByteArray.size())
        {


            ui->serial_show->append(sendData_ByteArray);

            QString tmp_String=sendData_ByteArray;
            QStringList str_1=tmp_String.split(" ");
            qDebug()<<str_1;

            series1->append(x_index,str_1[0].toFloat());
            series2->append(x_index,ui->key_lineEdit->text().toFloat());
            //if(x_index > 10)
            //    axisX->setRange(0, x_index);

            if(x_index > 10)
                axisX->setRange(x_index-10, x_index);
            //if(x_index > 10)
            //    chart->scroll(x_index,0);
            x_index ++;
            qDebug()<<"x_index:"<<x_index;
        }
    }



}
void MainWindow::textclear()
{
     ui->serial_show->clear();
}
void MainWindow::checkserial()
{
    qDebug()<<"checkserial";
    ui->serial_port->clear();
    QList<QSerialPortInfo> serialPortInfoList = QSerialPortInfo::availablePorts();

    foreach(QSerialPortInfo serialPortInfo,serialPortInfoList)  //打印出端口信息
    {
        QString strComboShow =serialPortInfo.portName();// (serialPortInfo.portName() + "：" + serialPortInfo.description());
        //qDebug()<<strComboShow;
        ui->serial_port->addItem(strComboShow);
    }
}
void MainWindow::openserial()
{
    if(0==get_open_flag())
    {
        //qDebug()<<"open_flag is 0";
        //open serial;
        /*********************************************************/
        //serial_port
        serial->setPortName(ui->serial_port->currentText());

        switch(ui->boud_munber->currentIndex())
        {
            case 0:
                serial->setBaudRate(QSerialPort::Baud115200);
            break;
            case 1:
                serial->setBaudRate(QSerialPort::Baud9600);
            break;
        }
        //data_num
        switch(ui->data_num->currentIndex())
        {
            case 0://8
                serial->setDataBits(QSerialPort::Data8);
            break;
            case 1://5
                serial->setDataBits(QSerialPort::Data5);
            break;
            case 2://6
                serial->setDataBits(QSerialPort::Data6);
            break;
            case 3://7
                serial->setDataBits(QSerialPort::Data7);
            break;
        }
        //stop_bit
        switch(ui->stop_bit->currentIndex())
        {
            case 0://1
                serial->setStopBits(QSerialPort::OneStop);
            break;
            case 1://1.5
                serial->setStopBits(QSerialPort::OneAndHalfStop);
            break;
            case 2://2
                serial->setStopBits(QSerialPort::TwoStop);
            break;
        }
        //check_bit
        switch(ui->check_bit->currentIndex())
        {
            case 0:
                serial->setParity(QSerialPort::NoParity);
            break;
            case 1:
                serial->setParity(QSerialPort::OddParity);
            break;
            case 2:
                serial->setParity(QSerialPort::EvenParity);
            break;
            case 3:
                serial->setParity(QSerialPort::MarkParity);
            break;
            case 4:
                serial->setParity(QSerialPort::SpaceParity);
            break;
        }
        //serial_contron
        //ui->Net_S_FlowCombo->addItem(tr("RTS/CTS"), QSerialPort::HardwareControl);
        //ui->Net_S_FlowCombo->addItem(tr("XON/XOFF"), QSerialPort::SoftwareControl);
        switch(ui->serial_contron->currentIndex())
        {
            case 0:
                serial->setFlowControl(QSerialPort::NoFlowControl);
            break;
            case 1:

            break;
            case 2:

            break;
            case 3:

            break;

        }

        if(!serial->open(QIODevice::ReadWrite))//用ReadWrite 的模式尝试打开串口
        {
            qDebug()<<"fail";
            return;
        }
        else
        {
            ui->serial_port->setEditable(false);
            ui->boud_munber->setEditable(false);
            ui->data_num->setEditable(false);
            ui->stop_bit->setEditable(false);
            ui->check_bit->setEditable(false);
            ui->serial_contron->setEditable(false);

            ui->open_button->setText("关闭");
            qDebug()<<ui->boud_munber->currentText();
            open_flag=1;
        }


    }
    else
    {
        if(serial->isOpen())
        {
            serial->clear();
            serial->close();
        }

        ui->serial_port->setEditable(true);
        ui->boud_munber->setEditable(true);
        ui->data_num->setEditable(true);
        ui->stop_bit->setEditable(true);
        ui->check_bit->setEditable(true);
        ui->serial_contron->setEditable(true);

        qDebug()<<"open_flag is 1";
        ui->open_button->setText("打开");
        open_flag=0;
    }
}
